﻿using Common;
using IBLL;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Unmanned_MSUI.Areas.Admin.Filters;

namespace Unmanned_MSUI.Areas.Admin.Controllers
{
    [Area("Admin")]
    /*   [MyFilter]*/
    public class CommodityInfoController : Controller
    {
        private ICommodityInfoBLL _commodityInfoBLL;

        public CommodityInfoController(
            ICommodityInfoBLL commodityInfoBLL
            )
        {
            _commodityInfoBLL = commodityInfoBLL;
        }

        #region 视图
        public IActionResult ListView()
        {
            return View();
        }

        public IActionResult CreateCommodityInfoView()
        {
            return View();
        }

        public IActionResult UpdateCommodityInfoView()
        {
            return View();
        }
        #endregion

        #region 新增(CreateCommodityInfo)
        [HttpPost]
        public IActionResult CreateCommodityInfo([FromForm] CommodityInfo commodityInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //添加商品的方法
            string msg;
            result.IsSuccess = _commodityInfoBLL.CreateCommodityInfo(commodityInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion

        #region 获取(GetCommodityInfos)
        public IActionResult GetCommodityInfoList(int page, int limit, string commodityName)
        {
            //声明返回对象
            ReturnResult result = new ReturnResult();
            //声明一个总数的变量
            int count;
            //调用BLL的获取用户方法
            var list = _commodityInfoBLL.GetCommodityInfoList(page, limit, commodityName, out count);
            //返回值
            result.Code = 0;
            result.IsSuccess = true;
            result.Msg = "获取成功！";
            result.Count = count;
            result.Data = list;


            //返回结果
            return Json(result);
        }
        #endregion

        #region 修改(UpdateCommodityInfo)
        public IActionResult UpdateCommodityInfo([FromForm] CommodityInfo commodityInfo)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //调用添加部门的方法
            string msg;
            result.IsSuccess = _commodityInfoBLL.UpdateCommodityInfo(commodityInfo, out msg);
            result.Msg = msg;
            //判断是否添加成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);
        }
        #endregion

        #region 删除(DeleteCommodityInfo)
        public IActionResult DeleteCommodityInfo(string id)
        {
            //实例化返回结果
            ReturnResult result = new ReturnResult();

            //非空判断
            if (string.IsNullOrWhiteSpace(id))
            {
                result.Msg = "id不能为空！";
                return Json(result);
            }
            //调用删除方法
            bool isOk = _commodityInfoBLL.DeleteCommodityInfo(id);
            //判断删除情况
            if (isOk)
            {
                result.Msg = "删除成功!";
                result.Code = 200;
            }
            //返回结果
            return Json(result);
        }
        #endregion

        //#region 批量删除(DeleteCommodityInfos)
        //public IActionResult DeleteCommodityInfos(List<string> ids)
        //{
        //    //实例化返回对象
        //    ReturnResult result = new ReturnResult();
        //    //判断非空

        //    if (ids == null || ids.Count == 0)
        //    {
        //        result.Msg = "你还没有选择要删除的商品！";
        //        return Json(result);
        //    }
        //    //调用批量删除的方法
        //    bool isOk = _commodityInfoBLL.DeleteCommodityInfos(ids);
        //    //判断是否成功
        //    if (isOk)
        //    {
        //        result.Msg = "删除成功！";
        //        result.Code = 200;
        //    }
        //    else
        //    {
        //        result.Msg = "删除失败";
        //    }
        //    return Json(result);//返回结果
        //}
        //#endregion

        #region Excel上传库的接口(Upload)
        public IActionResult Upload(IFormFile file)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            //先获取当前登录的id
            var userId = HttpContext.Request.Cookies["UserId"];
            if (userId == null || string.IsNullOrWhiteSpace(userId))
            {
                result.Msg = "上传用户的id不存在";
                return Json(result);
            }

            //判断非空
            if (file == null)
            {
                result.Msg = "上传的文件为空";
                return Json(result);
            }
            //获取一下文件名
            string fileName = file.FileName;
            string extension = Path.GetExtension(fileName);//文件名的后缀
            //判断文件的后缀是否非Excel文件
            if (extension == ".xls" || extension == ".xlsx")
            {
                Stream stream = file.OpenReadStream();//获取传递进来的文件流
                string msg;//消息返回

                //调用BLL的方法
                bool isOk = _commodityInfoBLL.Upload(stream, extension, userId, out msg);

                result.IsSuccess = isOk;
                result.Msg = msg;
                if (isOk)
                {
                    result.Code = 200;
                }
                return Json(result);
            }
            else
            {
                result.Msg = "上传的文件只能是Excel类型";
                return Json(result);
            }
        }
        #endregion

        #region 导出(GetDowload)
        public IActionResult GetDowload()
        {

            var staffName = HttpContext.Request.Cookies["StaffName"];
            string dowloadFileName;

            Stream stream = _commodityInfoBLL.GetDowload(out dowloadFileName, staffName);

            return File(stream, "application/octet-stream", dowloadFileName );
        }
        #endregion

        #region 购买
        [HttpPost]
        public IActionResult Purchase(string productId)
        {
            //实例化返回对象
            ReturnResult result = new ReturnResult();
            var userId = HttpContext.Request.Cookies["UserId"];
            var userName = HttpContext.Request.Cookies["UserName"];
            //添加商品的方法
            string msg;
            result.IsSuccess = _commodityInfoBLL.Purchase(userId, userName, productId, out msg);
            result.Msg = msg;
            //判断是否购买成功并返回结果
            if (result.IsSuccess)
            {
                result.Code = 200;//修改状态码
            }
            return Json(result);

        }
        #endregion


    }
}
